/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.toHouse.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.toHouse.domain.ReceiptDetail;
import me.zhengjie.suli.toHouse.repository.ReceiptDetailRepository;
import me.zhengjie.suli.toHouse.service.ReceiptDetailService;
import me.zhengjie.suli.toHouse.service.dto.ReceiptDetailDto;
import me.zhengjie.suli.toHouse.service.dto.ReceiptDetailQueryCriteria;
import me.zhengjie.suli.toHouse.service.mapstruct.ReceiptDetailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wyh
* @date 2024-02-01
**/
@Service
@RequiredArgsConstructor
public class ReceiptDetailServiceImpl implements ReceiptDetailService {

    private final ReceiptDetailRepository receiptDetailRepository;
    private final ReceiptDetailMapper receiptDetailMapper;

    @Override
    public Map<String,Object> queryAll(ReceiptDetailQueryCriteria criteria, Pageable pageable){
        Page<ReceiptDetail> page = receiptDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(receiptDetailMapper::toDto));
    }

    @Override
    public List<ReceiptDetailDto> queryAll(ReceiptDetailQueryCriteria criteria){
        return receiptDetailMapper.toDto(receiptDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReceiptDetailDto findById(String id) {
        ReceiptDetail receiptDetail = receiptDetailRepository.findById(id).orElseGet(ReceiptDetail::new);
        ValidationUtil.isNull(receiptDetail.getId(),"ReceiptDetail","id",id);
        return receiptDetailMapper.toDto(receiptDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReceiptDetailDto create(ReceiptDetail resources) {
        return receiptDetailMapper.toDto(receiptDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReceiptDetail resources) {
        ReceiptDetail receiptDetail = receiptDetailRepository.findById(resources.getId()).orElseGet(ReceiptDetail::new);
        ValidationUtil.isNull( receiptDetail.getId(),"ReceiptDetail","id",resources.getId());
        receiptDetail.copy(resources);
        receiptDetailRepository.save(receiptDetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            receiptDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ReceiptDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReceiptDetailDto receiptDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("入库单号", receiptDetail.getMtList());
            map.put("采购订单号", receiptDetail.getChNo());
            map.put("物料编号", receiptDetail.getElNo());
            map.put("物料名称", receiptDetail.getElName());
            map.put("入库数量", receiptDetail.getMtQty());
            map.put("单位", receiptDetail.getElUnit());
            map.put("入库库位", receiptDetail.getMtArea());
            map.put("工厂", receiptDetail.getWaNo());
            map.put("批次号", receiptDetail.getLotNo());
            map.put("箱条号码", receiptDetail.getPkList());
            map.put("物料数量", receiptDetail.getPkQty());
            map.put("是否VMI", receiptDetail.getVmiCode());
            map.put("操作人员", receiptDetail.getRtdUser());
            map.put("头表id", receiptDetail.getReceiptId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<ReceiptDetail> getReceiptDetailsById(String id) {
        List<ReceiptDetail> list = receiptDetailRepository.getReceiptDetailsById(id);
        return list;
    }
}